// pages/Bill/Bill.js
const app = getApp()
const utils = require('../../../utils/util.js')
const http = require('../../../utils/http.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    res: '',
    money_bill: '',
    tops: Number(getApp().globalData.statusBarHeight + 46),
    height: getApp().globalData.clientHeight,
    onPageTitle: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.height)
    // let group = options.group;
    // let ids = options.ids;
    // if (group) {
    //   this.setData({
    //     group: group,
    //     ids: ids
    //   })
    //   this.getBill({
    //     now_page:1,
    //     bill_type_group:0
    //   })
    // } else {
    //   this.getBill(1, 0, 0);
    //   this.setData({
    //     group: 0,
    //     ids: 0
    //   })
    // }
    this.setData({
      onPageTitle: options.title ? options.title : '账单明细'
    })
    this.bill({
      now_page: 1,
      bill_type_group: options.bill_type_group ? options.bill_type_group : 0,
      bill_type_ids: options.bill_type_ids ? options.bill_type_ids : 0
    })

  },
  move(e) {
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },
  onPageScroll(e) {},

  //获取账单列表
  bill(data) {
    let _this = this

    http.getRequest('v1/bill', data,
      function (res) {
        var money_bills = res.data.list;
        if (money_bills.length > 0) {

          for (let i = 0; i < money_bills.length; i++) {
            for (let z = 0; z < money_bills[i].month_money_bill.money_bill.length; z++) {
              money_bills[i].month_money_bill.money_bill[z].create_time = utils.formatTime(money_bills[i].month_money_bill.money_bill[z].create_time, 'M-D h:m:s');
              money_bills[i].month_money_bill.money_bill[z].enter = money_bills[i].month_money_bill.enter_money_total;
              money_bills[i].month_money_bill.money_bill[z].out = money_bills[i].month_money_bill.out_money_total;

            }
          }
          _this.setData({
            money_bill: money_bills,
            now_page: res.data.now_page,
            month: money_bills[0].month_money_bill.money_bill[0].month,
            enter: money_bills[0].month_money_bill.enter_money_total,
            out: money_bills[0].month_money_bill.out_money_total
          })

        }
      },
      function (res) {
        _this.toastDialog.showDialog(res.msg)

      })
  },

  //获取当前账单信息详情
  getDetail(e) {
    var bill_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/onNewPages/BillDetail/BillDetail?id=' + bill_id,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  BindMove(e) {
    let month = e.currentTarget.dataset.month;
    let money_bill = this.data.money_bill;
    let enter = e.currentTarget.dataset.enter;
    let out = e.currentTarget.dataset.out;
    this.setData({
      money_bill: money_bill,
      month: month,
      enter: enter,
      out: out
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})